//
//  RelatePersonTableViewCell.m
//  HuaRun2
//
//  Created by admin on 17/8/29.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "RelatePersonTableViewCell.h"
#import "ContactPersonModel.h"

@interface RelatePersonTableViewCell ()

@property (nonatomic, strong) UIView * backView;

@property (nonatomic, strong) UIButton * firstCharBtn;
//名字
@property (nonatomic, strong) UILabel * nameLab;
//公司
@property (nonatomic, strong) UILabel * companyLab;

@end

@implementation RelatePersonTableViewCell

#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = RGBA(249, 249, 249, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self viewInit];
        [self layoutInit];
//        [self testMethod];
    }
    return self;
}

- (void)viewInit {
    
    [self.contentView addSubview:self.backView];

    [self.contentView addSubview:self.firstCharBtn];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.companyLab];
    
}

- (void)layoutInit {
    
    weakself(ws);
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.contentView).offset(kMargin10);
        make.right.mas_equalTo(ws.contentView).offset(-kMargin10);
        make.top.mas_equalTo(ws.contentView).offset(kMargin10);
        make.bottom.mas_equalTo(ws.contentView);
    }];

    [self.firstCharBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView.mas_left).offset(kMargin10);
        make.top.mas_equalTo(ws.nameLab).offset(kMargin10);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.firstCharBtn.mas_right).offset(kMargin10);
        make.top.mas_equalTo(ws.backView).offset(kMargin10);
        make.right.mas_equalTo(ws.mas_right).offset(-kMargin10);
        
    }];
    
    [self.companyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.nameLab);
        make.top.mas_equalTo(ws.nameLab.mas_bottom).offset(kMargin10);
        make.right.mas_equalTo(ws.nameLab);
    }];

}


//- (void)testMethod {
//    [_firstCharBtn setTitle:@"A" forState:(UIControlStateNormal)];
//    _nameLab.text = [NSString stringWithFormat:@"Admin %@", kTestStr];
//    _companyLab.text = kTestStr;
//    
//}

-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

#pragma mark - setter getter
- (void)setContactPersonModel:(ContactPersonModel *)contactPersonModel {
    _contactPersonModel = contactPersonModel;
    
    //    中文名是：nameChinese英文名是  lastName + firstName
    //     title department accountName
    _nameLab.text =  [NSString stringWithFormat:@"%@ %@\n%@",
                      [NSString showStringWithStr: contactPersonModel.lastName],
                      [NSString showStringWithStr: contactPersonModel.firstName],
                      [NSString showStringWithStr: contactPersonModel.nameChinese]];
    
#pragma mark 测试
    _companyLab.text =  [NSString stringWithFormat:@"%@\n%@\n%@",
                         [NSString showStringWithStr: contactPersonModel.title],
                         [NSString showStringWithStr: contactPersonModel.department],
                         [NSString showStringWithStr: contactPersonModel.department]];
    if (contactPersonModel.firstName.length > 1) {
        NSString * firstStr = [[contactPersonModel.firstName substringToIndex:1]
                               uppercaseString];
        [_firstCharBtn setTitle:[NSString showStringWithStr:firstStr]
                       forState:(UIControlStateNormal)];
    }else if (contactPersonModel.lastName.length  > 1) {
        NSString * firstStr = [[contactPersonModel.lastName substringToIndex:1]
                               uppercaseString];
        [_firstCharBtn setTitle:[NSString showSpaceStringWithStr:firstStr]
                       forState:(UIControlStateNormal)];
    }
}
- (UIButton *)firstCharBtn {
    if (!_firstCharBtn) {
        self.firstCharBtn = [UIButton new];
        _firstCharBtn.titleLabel.font = kFontSize10;
        _firstCharBtn.titleLabel.textColor = [UIColor whiteColor];
        
        int n = [self getRandomNumber:1 to:8];
        NSString * str = [NSString stringWithFormat:@"0%d", n];
        [_firstCharBtn setBackgroundImage:[UIImage imageNamed:str]
                                 forState:(UIControlStateNormal)];
    }
    return _firstCharBtn;
}


- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.font = kGeneralFontSize;
        _nameLab.textColor = [UIColor blackColor];
        _nameLab.numberOfLines = 0;
    }
    return _nameLab;
}

- (UILabel *)companyLab {
    if (!_companyLab) {
        self.companyLab = [UILabel new];
        _companyLab.textAlignment = NSTextAlignmentLeft;
        _companyLab.font = kGeneralFontSize;
        _companyLab.textColor = [UIColor grayColor];
    }
    return _companyLab;
}
- (UIView *)backView {
    if (!_backView) {
        self.backView = [UIView new];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

@end
